package com.moody.init;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.moody.hibernate.domain.CodeLib;

/*
 * Copyright © Moody analytics
 *
 * @Author: Yan Jian
 * @Time: 2011-9-14 下午04:18:24
 * @Modified by: 
 * @Description: 行业初始数据处理
 */
public class Industry {
	private static final Logger log = LoggerFactory
			.getLogger(DataDictionary.class);
	private static Industry instance = null;
	private static Map<String, Map<String, String>> industryTreeMap = new LinkedHashMap<String, Map<String, String>>();
	private static Map<String, String> industryMap = new LinkedHashMap<String, String>();
	private static LinkedList<LinkedHashMap<String, String>> industryLevelOneList = new LinkedList<LinkedHashMap<String, String>>();

	private Industry() {

	}

	public static Industry getInstance() {
		if (instance == null) {
			instance = new Industry();
		}
		return instance;
	}

	public Map<String, String> getIndustry() {
		Map<String, CodeLib> industry = new LinkedHashMap<String, CodeLib>();
		industry = DataDictionary.getInstance().getDataDictionary()
				.get("IndustryType");
		Iterator iter = industry.entrySet().iterator();
		while (iter.hasNext()) {
			Map.Entry entry = (Map.Entry) iter.next();
			String industryCode = (String) entry.getKey();
			String industryName = ((CodeLib) entry.getValue()).getItemName();
			industryMap.put(industryCode, industryName);
		}
		return industryMap;
	}

	public Map<String, Map<String, String>> getIndustryTree() {
		Map<String, CodeLib> industry = new LinkedHashMap<String, CodeLib>();

		industry = DataDictionary.getInstance().getDataDictionary()
				.get("IndustryType");

		Iterator iter = industry.entrySet().iterator();
		while (iter.hasNext()) {
			Map.Entry entry = (Map.Entry) iter.next();
			String industryCode = (String) entry.getKey();
			String industryName = ((CodeLib) entry.getValue()).getItemName();
			if (industryCode.length() == 1
					&& !industryTreeMap.containsKey(industryCode)) {
				industryTreeMap.put(industryCode,
						new LinkedHashMap<String, String>());
			} else if (industryCode.length() == 3) {
				String industryCodeLevelOne = industryCode.substring(0, 1);
				// String industryNameLevelOne =
				// industry.get(industryCodeLevelOne);
				if (industryTreeMap.containsKey(industryCodeLevelOne)
						&& !industryTreeMap.get(industryCodeLevelOne)
								.containsKey(industryCode)) {
					industryTreeMap.get(industryCodeLevelOne).put(industryCode,
							industryName);
				} else if (!industryTreeMap.containsKey(industryCodeLevelOne)) {
					Map<String, String> industryLevelTwo = new LinkedHashMap<String, String>();

					industryLevelTwo.put(industryCode, industryName);
					industryTreeMap.put(industryCodeLevelOne, industryLevelTwo);

				}
			}

		}
		return industryTreeMap;
	}

	@SuppressWarnings("unchecked")
	public LinkedList<LinkedHashMap<String, String>> getIndustryLevelOne() {
		if (industryLevelOneList.size() > 0)
			return industryLevelOneList;
		else {
			Map<String, CodeLib> industry = DataDictionary.getInstance()
					.getDataDictionary().get("IndustryType");

			Iterator iter = industry.entrySet().iterator();
			while (iter.hasNext()) {
				Map.Entry<String, CodeLib> entry = (Entry<String, CodeLib>) iter
						.next();
				String industryCode = (String) entry.getKey();
				String industryName = ((CodeLib) entry.getValue())
						.getItemName();
				if (industryCode.length() == 1) {
					LinkedHashMap<String, String> itemMap = new LinkedHashMap<String, String>();
					itemMap.put("code", industryCode);
					itemMap.put("name", industryName);
					industryLevelOneList.add(itemMap);
				}
			}
			return industryLevelOneList;
		}
	}

	public List<Map<String, String>> getIndustryLevelTwo(String levelOneId) {
		List<Map<String, String>> levelTwoList = new LinkedList<Map<String, String>>();
		getIndustryTree();
		Map<String, String> levelTwo = industryTreeMap.get(levelOneId);
		Iterator iter = levelTwo.entrySet().iterator();
		while (iter.hasNext()) {
			Map.Entry<String, String> entry = (Map.Entry<String, String>) iter
					.next();
			String code = entry.getKey();
			String name = entry.getValue();
			Map<String, String> itemMap = new LinkedHashMap<String, String>();
			itemMap.put("code", code);
			itemMap.put("name", name);
			levelTwoList.add(itemMap);
		}
		return levelTwoList;
	}
}
